<?php

namespace Home\Controller;

use Home\Common\Common;

class ApplicationController extends Common
{
    public function show()
    {
        //请假申请表
        $application = M('applicationinfo');
        //搜索
        if (empty($_GET['search']) || empty($_GET['value'])) {
            $where = 'applicationinfo.stf_id='.$_SESSION['aid'];
        } else {
            //where条件的附近一定要加上空格
            $where = 'applicationinfo.stf_id='.$_SESSION['aid'].'and stf_name like "%' . $_GET['value'] . '%"';
            $this->assign('value', $_GET['value']);
        }
        $count = $application->join('staffinfo on staffinfo.stf_id=applicationinfo.stf_id')->where($where)->count();  //统计输出到页面的数据总条数
        $page = new \Think\Page($count, 8); //实例化thinkphp的分页类
        $show = $page->show(); //输出分页
        //limit('0,5')   limit('6,10')
        //连表查询数据[主表（调岗表)关联子表(员工表、部门表、岗位表)]
        $applicationList = $application->join('staffinfo on staffinfo.stf_id=applicationinfo.stf_id')->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('at_id')->select();
        $this->assign('count', $count);  //将计数的数据输出到视图
        $this->assign('show', $show);  //将分页信息输出到视图
        $this->assign('applicationList', $applicationList);  //将数据输出到视图
        //员工表（需要查询请假的人）
        $staff = M('staffinfo');
        $staffList = $staff->select();
        $this->assign('staffList', $staffList);
       //类型表
        $type = M('atrtypeinfo');
        $typeList = $type->select();
        $this->assign('typeList', $typeList);
        $this->display();
    }

    /**
     * 添加要请假的人
     */
    public function add()
    {
        /**
         * 查询员工表
         */
        if (empty($_POST['submit'])) {
            //员工表
            $staff = M('staffinfo');
            $staffList = $staff->select();
            $this->assign('staffList', $staffList);
           //请假类型表
            $type = M('atrtypeinfo');
            $typeList = $type->select();
            $this->assign('typeList', $typeList);
            $this->display();
        } else {
            $application = M('applicationinfo');




            // strtotime将任何形式的时间日期转换成对应的Unix 时间戳。
            $start = strtotime($_POST['at_starttime']);//开始时间
            $end = strtotime($_POST['at_endtime']);//结束时间
            if ($start < $end) {
                $cnt = $end - $start;//与已知时间的差值
                $days = floor($cnt / (3600 * 24));//算出天数
            } else if ($start > $end) {
                $this->error('请假的开始时间应该小于结束时间');
            }
            if(empty($_POST['at_starttime'])&& empty($_POST['at_endtime'])){
                $this->error('请填写请假时间', U('add'));
            }
            $_POST['at_duration'] = $days;
            $_POST['dpt_id']=$this->_listprid['dpt_id'];
            $_POST['status']=1;

            $setinfo = M('setinfo'); //流程设置表
            $asCount = $setinfo->where('`artid`='.$_POST['at_type'].' and `states`=1')->count();
            $levelInfo = $setinfo->where('`artid`='.$_POST['at_type'].' and `pt_id`='.$this->_listprid['pt_id'])->find();

            if (empty($levelInfo)){
                $_POST['status'] = 1;
            }else{
                if ($levelInfo['sort'] == $asCount){
                    $this->error('您的级别，无需申请');
                }else{
                    $_POST['status'] = $levelInfo['sort'] + 1;
                }
            }


            $application->add($_POST);//添加到数据库
            $this->success('添加成功', U('show'));
        }
    }
    /**
     * 删除单条数据
     */
    public function del()
    {
        if (empty($_GET['at_id'])) {
            $this->error('参数错误');
        }
        $application = M('applicationinfo');
        $application->delete($_GET['at_id']);
        $this->success('删除成功', U('show'));
    }

    /**
     * 删除多条数据
     */
    public function del_all()
    {
        if (empty($_GET['at_ids'])) {
            $this->error('请选择你要删除的数据');
        }
        $application = M('applicationinfo');
        foreach ($_GET['at_ids'] as $at_ids) {
            $application->delete($at_ids);
        }
        $this->success('删除成功', U('show'));
    }
}